package com.ux.iot.core.bo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

/**
 * @author oaux
 * @version 1.0
 * @date 2021/11/13 10:17
 * 返回结果
 */
@Builder
@AllArgsConstructor
@Data
public class R implements Serializable {

    Object data;

    String msg;

    Object errorCode;

    boolean success=true;

    public static R success(Object re){
        R responseResult=new R();
        responseResult.data=re;
        responseResult.success=true;
        responseResult.errorCode=ResultCode.SUCCESS.getCode();
        responseResult.setMsg(ResultCode.SUCCESS.getMsg());
        return responseResult;
    }

    public  R(){}
    public static R success(Object re,String msg){
        R responseResult=new R();
        responseResult.data=re;
        responseResult.success=true;
        responseResult.errorCode=re;
        responseResult.setMsg(msg);
        return responseResult;
    }
    public static R success(){
        R responseResult=new R();
        responseResult.data=null;
        responseResult.success=true;
        responseResult.errorCode=ResultCode.SUCCESS.getCode();
        responseResult.setMsg(ResultCode.SUCCESS.getMsg());
        return responseResult;
    }
    public static R fail(String msg){
        R responseResult=new R();
        responseResult.success=false;
        responseResult.errorCode=ResultCode.SYSTEM_ERROR_B0001.getCode();
        responseResult.setMsg(msg);
        return responseResult;
    }
    public static R fail(Object data){
        R responseResult=new R();
        responseResult.success=false;
        responseResult.errorCode=ResultCode.SYSTEM_ERROR_B0001.getCode();
        responseResult.setData(data);
        return responseResult;
    }
    public static R fail(ResultCode resultCode){
        R responseResult=new R();
        responseResult.success=false;
        responseResult.errorCode=resultCode.getCode();
        responseResult.setMsg(resultCode.getMsg());
        return responseResult;
    }
    public static R genResult(Object code,boolean success,String msg){
        R responseResult=new R();
        responseResult.success=success;
        responseResult.errorCode=code;
        responseResult.msg =msg;
        return responseResult;
    }
    public static R genResult(boolean success, ResultCode resultCode){
        R responseResult=new R();
        responseResult.success=success;
        responseResult.errorCode=resultCode.getCode();
        responseResult.msg =resultCode.getMsg();
        return responseResult;
    }
    public static R genFailEnum(ResultCode resultCode){
        R responseResult=new R();
        responseResult.success=false;
        responseResult.errorCode=resultCode.getCode();
        responseResult.setMsg(resultCode.getMsg());
        return responseResult;
    }

}
